package deanandjames.stats;

import com.sun.j3d.utils.timer.J3DTimer;

import java.awt.*;

public class StatisticsController {
	private static final int MAX_HITS = 10;
	private long gameStartTime;
	private int numHits = 0;
	private int score;
	private final FontMetrics fontMetrics;
	private final Font messageFont;
	private final int panelWidth;
	private final int panelHeight;

	public StatisticsController(FontMetrics fontMetrics, Font messageFont, int panelWidth, int panelHeight) {
		this.fontMetrics = fontMetrics;
		this.messageFont = messageFont;
		this.panelWidth = panelWidth;
		this.panelHeight = panelHeight;
	}

	public boolean exceededMaxNumberOfHits() {
		return numHits >= MAX_HITS;
	}

	public long getGameStartTime() {
		return gameStartTime;
	}

	public int getNumHits() {
		return numHits;
	}

	public void incrementNumHits() {
		numHits++;
	}

	public void paint(Graphics g, boolean isGameOver) {
		int timeSpentInGame = 0;
		if (!isGameOver) {
			timeSpentInGame = (int) ((J3DTimer.getValue() - gameStartTime) / 1000000000L);
		}
		g.setColor(Color.red);
		g.setFont(messageFont);
		g.drawString("Hits: " + numHits + "/" + MAX_HITS, 15, 25);
		g.drawString("Time: " + timeSpentInGame + " secs", 15, 50);
		g.setColor(Color.black);
	}

	public void paintGameOver(Graphics g) {
		final String msg = "Game Over. Your score: " + score;
		final int x = (panelWidth - fontMetrics.stringWidth(msg)) / 2;
		final int y = (panelHeight - fontMetrics.getHeight()) / 2;
		g.setColor(Color.black);
		g.setFont(messageFont);
		g.drawString(msg, x, y);
	}

	public void setGameStartTime(long gameStartTime) {
		this.gameStartTime = gameStartTime;
	}

	public void setScore(int score) {
		this.score = score;
	}
}
